

********************************************************
** Figure 4
********************************************************

clear
clear matrix
clear mata

global data="C:\CFLs\Data\"
global outputs="C:\CFLs\Outputs\"

set maxvar 10000
cd "$data"

use "hh_panel_trtmt.dta"  , clear // REQUIRES ELECTRICITY UTILITY DATA 


* Drop outliers: top 1% of observations each month (w.r.t. electricity bill) 
replace elec=. if elec_xt10_==1  
replace diff_pred_act=. if elec_xt10_==1  
replace elec=. if elec==0

* Drop movers: households that moved  between intervention and follow-up survey (101 households)
drop if movers_v1== 1  /*leaves: 899 HHs */

* Limit to period of 2 years prior to intervention
drop if time<201104

* Limit to period before tariff change 
keep if time<201410



global baseelec1yr=" elec201303 elec201302 elec201301 elec201212 elec201211 elec201210 elec201209 elec201208 elec201205 elec201204 "

xtset hh_id time
save temp_elec.dta , replace 


***************************************************************************************************
use  temp_elec.dta , clear

****** 1: Impacts - ACTUAL (control for 1 year baseline elec consumption) 
capture: reg elec _TXtime_* i.time _tgtreat_CXtime_*       elec201303 elec201302 elec201301 elec201212 elec201211 elec201210  elec201209 elec201208 elec201207 elec201206 elec201205 elec201204    collect_days        				 , vce(cl hh_id)  				 
quietly parmest, stars(0.10 0.05 0.01) saving("Figure_elec_28",replace) 

****** 2: Impacts - PREDICTED 	(control for 1 year baseline elec consumption) 		
capture: reg pre_elecsav _TXtime_* i.time         elec201303 elec201302 elec201301 elec201212 elec201211 elec201210  elec201209 elec201208 elec201207 elec201206 elec201205 elec201204  collect_days   , vce(cl hh_id)  				 
		quietly parmest, stars(0.10 0.05 0.01) saving("Figure_elec_48",replace)   	  


***************************************************************************************************
*** Graphing Treatment observations


cd "$data"

foreach i in 28 48 {
cd "$data"
	use "Figure_elec_`i'.dta", clear
	keep parm estimate min95 max95 p stars
	g keep=strmatch(parm,"*Xtime_*") 
	drop if keep==0
	drop keep
	gen time=parm
		
		replace time=subinstr(time,"_TXtime_", "", . )
		replace time=subinstr(time,"_T_Xtime_", "", . )
		
	replace time=subinstr(time,"b", "-", . )
	replace time=subinstr(time,"a", "", . )
	sort time parm
	list
*Creating date variable
	g month=substr(time,-2,.)
	g year=substr(time,1,4)
	tempvar date 
	g `date' = month+"/"+year
	g date=date(`date', "M20Y")
	format date %tdmy
	destring time year month, replace force	
	drop _merge 
	sort time 
	drop if time==.
*--
save, replace
}




foreach i in  28 48 {
cd "$data"
use "Figure_elec_`i'.dta", clear
cd "$outputs"
list
capture: drop __00000*
tempvar a 
g `a'= strmatch(parm,"_TXtime_*")
keep if `a' ==1
capture: sort time
tsset  date, daily
local date="01jun2012 01sep2012 01dec2012 01mar2013 01jun2013 01sep2013 01dec2013 01mar2014 01jun2014 01sep2014"
twoway (tsline estimate min95 max95 if tin(01jun2012,01sep2014), yaxis(1) lpattern(solid dash dash) lcolor(black black black) ), ///
         graphregion(color(white)) yscale(range(-100 100)) ylab(-100(50)100, nogrid angle(0)) tlabel(`date', format(%tdmy) nogrid angle(90) ) ///
         ytitle() legend(lab(1 "Coefficient on T") lab(2 "95 % Confidence intervals") order(1 2)) ///
         xtitle("") legend(rows(1)) legend(region(lcolor(white))) yline(0) title(T) saving(Figure_elec_`i', replace)
capture: graph export "Figure_elec_`i'.pdf", as(pdf) replace
}



***************************************************************************************************
*** Graph - Actual versus predicted electricity savings 

/* ALL T */ 
cd "$data"
use "Figure_elec_48.dta", clear   /*Predicted*/ 
count 

list
capture: drop __00000*
rename estimate estimate_p 
keep time estimate_p
drop if time==.
sort time 
merge 1:1 time using "Figure_elec_28.dta"
drop _merge
/*gen est_diff = estimate - estimate_p
replace est_diff=. if time<201304*/

*Graph Actual AND Predicted  
cd "$outputs"
list 
capture: drop __00000*
tempvar a 
g `a'= strmatch(parm,"_TXtime_*")
keep if `a' ==1
capture: sort time
tsset  date, daily
local date=" 01jun2012 01sep2012 01dec2012 01mar2013 01jun2013 01sep2013 01dec2013 01mar2014 01jun2014 01sep2014 "
twoway (tsline estimate_p estimate min95 max95 if tin(01jun2012,01sep2014), yaxis(1) lpattern(solid solid dash dash) lcolor(green blue gray gray) ) , ///
        graphregion(color(white)) yscale(range(-100 100)) ylab(-100(50)100, nogrid angle(0)) tlabel(`date', format(%tdmy) nogrid angle(90) ) ///
		ytitle("kWh per month") legend(lab(1 "Predicted impact") lab(2 "Actual impact") lab(3 "95 % Conf int") order(1 2 3)) ///
        xtitle("") legend(rows(1)) legend(region(lcolor(white))) yline(0) title(T) saving(Figure_elec_`i', replace)
capture: graph export "Figure_elec_act+pred.pdf", as(pdf) replace






